package com.sinothk.special.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.base.model.BizUserInfo;
import com.sinothk.base.model.BizUserInfoDTO;
import com.sinothk.special.buyer.userblog.mapper.UserBlogTipMapper;
import com.sinothk.special.friendship.entity.FriendshipLink;
import com.sinothk.special.friendship.mapper.FriendshipLinkMapper;
import com.sinothk.special.user.entity.BizUserInfoForHomeVO;
import com.sinothk.special.user.mapper.BizUserInfoMapper;
import com.sinothk.special.user.service.IBizUserInfoService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 用户信息
 * @Author: Liangyt
 * @Date: 2022-03-29
 * @Version: V1.0
 */
@Service
public class BizUserInfoServiceImpl extends ServiceImpl<BizUserInfoMapper, BizUserInfo> implements IBizUserInfoService {
//    @Autowired
//    private ISysUserService sysUserService;
    @Resource
    private BizUserInfoMapper bizUserInfoMapper;
    @Resource
    private FriendshipLinkMapper friendshipLinkMapper;
    @Resource
    private UserBlogTipMapper userBlogTipMapper;

    @Override
    public void updateBizUser(BizUserInfo bizUserInfo) {
        bizUserInfo.setUid(bizUserInfo.getId());

        this.saveOrUpdate(bizUserInfo);
    }

//    @Override
//    public void updateUserState() {
//        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//
//        BizUserInfo bizUserInfoNew = new BizUserInfo();
//        bizUserInfoNew.setId(loginUser.getId());
//        bizUserInfoNew.setLoginTime(new Date());
//
//        this.saveOrUpdate(bizUserInfoNew);
//    }

    @Override
    public BizUserInfoDTO getUserInfoForOther(String userId) {
        BizUserInfoDTO bizUserInfoDTO = new BizUserInfoDTO();

        BizUserInfo bizUserInfo = bizUserInfoMapper.getBizUserInfoForOtherMain(userId);
        bizUserInfoDTO.setBizUserInfo(bizUserInfo);

        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String mainId = loginUser.getId();
        LambdaQueryWrapper<FriendshipLink> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(FriendshipLink::getMainUid, mainId)
                .eq(FriendshipLink::getFriendUid, userId);
        FriendshipLink link = friendshipLinkMapper.selectOne(linkLambdaQueryWrapper);
        if (link == null) {
            bizUserInfoDTO.setFriendship(0);
        } else {
            bizUserInfoDTO.setFriendship(link.getFriendShip());
        }

        return bizUserInfoDTO;
    }

    @Override
    public BizUserInfoForHomeVO editUserBizInfo(BizUserInfo userInfoVo) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        userInfoVo.setId(loginUser.getId());
//        try {
//            SysUser sysUser = sysUserService.getById(userInfoVo.getId());
//            userInfoVo.setUsername(sysUser.getUsername());
//            userInfoVo.setPhone(sysUser.getPhone());
//        } catch (Exception e) {
//            log.error(e.getMessage());
//        }

        BizUserInfo userInfo = new BizUserInfo();
        BeanUtils.copyProperties(userInfoVo, userInfo);
        userInfoVo.setUid(userInfoVo.getId());
        userInfo.setLoginTime(new Date());
        this.saveOrUpdate(userInfo);

        // 返回用户的最新信息
        BizUserInfoForHomeVO bizUserInfoForHomeVO = new BizUserInfoForHomeVO();
        BizUserInfo bizUserInfo = bizUserInfoMapper.getBizUserInfoForOtherMain(loginUser.getId());
        bizUserInfoForHomeVO.setBizUserInfo(bizUserInfo);

        // 关注的人的未读博客
        int blogTips = userBlogTipMapper.countBlogTips(loginUser.getId());
        bizUserInfoForHomeVO.setBlogTips(blogTips);

        return bizUserInfoForHomeVO;
    }
}
